package com.fl.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fl.common.utils.PageUtils;
import com.fl.common.utils.Query;

import com.fl.gulimall.product.dao.CategoryDao;
import com.fl.gulimall.product.entity.CategoryEntity;
import com.fl.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        //1.查出所有分类
        List<CategoryEntity> entities = baseMapper.selectList(null);
//        2.组装成父子结构
//        2.1 查询一级菜单
        List<CategoryEntity> level1Menus = entities.stream().filter(x -> x.getParentCid() == 0)
                .map((menu)->{
                    menu.setChildren(getChlidren(menu,entities));
                    return menu;
                }).sorted((menu1,menu2)->{
                    return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
                }).collect(Collectors.toList());
        return level1Menus;
    }

    @Override
    public void removeMenuByids(List<Long> asList) {
        //Todo 检查菜单是否被引用
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[] findcategoryPath(Long groupId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParentPath(groupId, paths);
        Collections.reverse(parentPath);
        Long[] array = parentPath.toArray(new Long[parentPath.size()]);
        return array;
    }

    /**
     * 根据id查询父节点
     * @param groupId
     * @param paths
     * @return
     */
    private List<Long> findParentPath(Long groupId,List<Long> paths){
        CategoryEntity byId = this.getById(groupId);
        paths.add(byId.getCatId());
        if (byId.getParentCid()!=0){
            findParentPath(byId.getParentCid(),paths);
        }
        return paths;
    }

    //递归查询当前菜单下所有的子菜单
    List<CategoryEntity> getChlidren(CategoryEntity menu,List<CategoryEntity> all){
        List<CategoryEntity> chlidList = all.stream().filter(x -> {
            return x.getParentCid() == menu.getCatId();
        }).map(category -> {
            //找到当前菜单下的所有二级三级等菜单
            category.setChildren(getChlidren(category, all));
            return category;
        }).sorted((m1, m2) -> {
            return (m1.getSort()==null?0:m1.getSort()) - (m2.getSort()==null?0:m2.getSort());
        }).collect(Collectors.toList());
        return chlidList;
    }
}